package com.ischoolbar.programmer.servlet;

/**
 * @Author lin
 **/

import com.ischoolbar.programmer.dao.AdminDao;
import com.ischoolbar.programmer.dao.StudentDao;
import com.ischoolbar.programmer.dao.TeacherDao;
import com.ischoolbar.programmer.model.Admin;
import com.ischoolbar.programmer.model.Student;
import com.ischoolbar.programmer.model.Teacher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SystemServlet extends HttpServlet {
    private static final long serialVersionUID = -7258264317769166483L;

    public SystemServlet() {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        if ("toPersonalView".equals(method)) {
            this.personalView(request, response);
        } else if ("EditPasswod".equals(method)) {
            this.editPassword(request, response);
        } else {
            try {
                request.getRequestDispatcher("view/system.jsp").forward(request, response);
            } catch (ServletException var5) {
                var5.printStackTrace();
            }

        }
    }

    private void editPassword(HttpServletRequest request, HttpServletResponse response) {
        String password = request.getParameter("password");
        String newPassword = request.getParameter("newpassword");
        response.setCharacterEncoding("UTF-8");
        int userType = Integer.parseInt(request.getSession().getAttribute("userType").toString());
        if (userType == 1) {
            Admin admin = (Admin)request.getSession().getAttribute("user");
            if (!admin.getPassword().equals(password)) {
                try {
                    response.getWriter().write("原密码错误！");
                    return;
                } catch (IOException var119) {
                    var119.printStackTrace();
                }
            }

            AdminDao adminDao = new AdminDao();
            if (adminDao.editPassword(admin, newPassword)) {
                try {
                    response.getWriter().write("success");
                } catch (IOException var115) {
                    var115.printStackTrace();
                } finally {
                    adminDao.closeCon();
                }
            } else {
                try {
                    response.getWriter().write("数据库修改错误");
                } catch (IOException var113) {
                    var113.printStackTrace();
                } finally {
                    adminDao.closeCon();
                }
            }
        }

        if (userType == 2) {
            Student student = (Student)request.getSession().getAttribute("user");
            if (!student.getPassword().equals(password)) {
                try {
                    response.getWriter().write("原密码错误！");
                    return;
                } catch (IOException var118) {
                    var118.printStackTrace();
                }
            }

            StudentDao studentDao = new StudentDao();
            if (studentDao.editPassword(student, newPassword)) {
                try {
                    response.getWriter().write("success");
                } catch (IOException var111) {
                    var111.printStackTrace();
                } finally {
                    studentDao.closeCon();
                }
            } else {
                try {
                    response.getWriter().write("数据库修改错误");
                } catch (IOException var109) {
                    var109.printStackTrace();
                } finally {
                    studentDao.closeCon();
                }
            }
        }

        if (userType == 3) {
            Teacher teacher = (Teacher)request.getSession().getAttribute("user");
            if (!teacher.getPassword().equals(password)) {
                try {
                    response.getWriter().write("原密码错误！");
                    return;
                } catch (IOException var117) {
                    var117.printStackTrace();
                }
            }

            TeacherDao teacherDao = new TeacherDao();
            if (teacherDao.editPassword(teacher, newPassword)) {
                try {
                    response.getWriter().write("success");
                } catch (IOException var107) {
                    var107.printStackTrace();
                } finally {
                    teacherDao.closeCon();
                }
            } else {
                try {
                    response.getWriter().write("数据库修改错误");
                } catch (IOException var105) {
                    var105.printStackTrace();
                } finally {
                    teacherDao.closeCon();
                }
            }
        }

    }

    private void personalView(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("view/personalView.jsp").forward(request, response);
        } catch (ServletException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }
}

